package com.maxd.respository.helpTask;

import com.maxd.model.HelpMaintain;
import com.maxd.model.HelpMaintainModel;
import com.maxd.model.HelpSendOrderDetails;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

public interface HelpMaintainRepository extends Repository<HelpMaintain, Long> {

    @Query(value = "select t.title,m.id,t.task_price as taskPrice,t.label,m.create_time as createTime,m.state,m.content from help_maintain m " +
                    "left join help_task t on t.id=m.help_task_id " +
                    "where send_order_user_id=:sendOrderUserId " +
                    "and m.state=0 "+
                    " group by m.id order by m.create_time desc",
                    countQuery="select count(*) from help_maintain m " +
                            "left join help_task t on t.id=m.help_task_id " +
                            "where send_order_user_id=:sendOrderUserId" +
                            "and m.state=0 "+
                            " group by m.id order by m.create_time desc",
                    nativeQuery = true)
    Page<Map<String,Object>> selectHelpMaintainListBySendOrder(@Param("sendOrderUserId") Long sendOrderUserId,Pageable pageable);

    @Query(value = "select t.title,m.id,t.task_price as taskPrice,t.label,m.create_time as createTime,m.state,m.content from help_maintain m " +
            "left join help_task t on t.id=m.help_task_id " +
            "where send_order_user_id=:sendOrderUserId " +
            "and m.state in (1,2) "+
            " group by m.id order by m.create_time desc",
            countQuery="select count(*) from help_maintain m " +
                    "left join help_task t on t.id=m.help_task_id " +
                    "where send_order_user_id=:sendOrderUserId " +
                    "and m.state in (1,2) "+
                    " group by m.id order by m.create_time desc",
            nativeQuery = true)
    Page<Map<String,Object>> selectHelpMaintainListBySendOrders(@Param("sendOrderUserId") Long sendOrderUserId,Pageable pageable);

    @Query(value = "select t.title,m.id,t.task_price as taskPrice,t.label,m.create_time as createTime,m.state,m.content from help_maintain m " +
            "left join help_task t on t.id=m.help_task_id " +
            "where take_order_user_id=:takeOrderUserId " +
            "and m.state in (1,2) "+
            "group by m.id order by m.create_time desc",
            countQuery="select count(*) from help_maintain m " +
                    "left join help_task t on t.id=m.help_task_id " +
                    "where take_order_user_id=:takeOrderUserId " +
                    "and m.state in (1,2) "+
                    "group by m.id order by m.create_time desc",
            nativeQuery = true)
    Page<Map<String,Object>> selectHelpMaintainListByTakeOrders(@Param("takeOrderUserId") Long takeOrderUserId, Pageable pageable);

    @Query(value = "select t.title,m.id,t.task_price as taskPrice,t.label,m.create_time as createTime,m.state,m.content from help_maintain m " +
            "left join help_task t on t.id=m.help_task_id " +
            "where take_order_user_id=:takeOrderUserId " +
            "and m.state=0 "+
            "group by m.id order by m.create_time desc",
            countQuery="select count(*) from help_maintain m " +
                    "left join help_task t on t.id=m.help_task_id " +
                    "where take_order_user_id=:takeOrderUserId " +
                    "and m.state=0 "+
                    "group by m.id order by m.create_time desc",
            nativeQuery = true)
    Page<Map<String,Object>> selectHelpMaintainListByTakeOrder(@Param("takeOrderUserId") Long takeOrderUserId, Pageable pageable);

    @Query(value = "select m.id as helpMaintainId, t.id as helpTaskId,t.title,t.task_price as taskPrice,t.label,m.create_time as createTime,m.state,m.content from help_maintain m " +
            "left join help_task t on t.id=m.help_task_id " +
            " group by m.id order by m.create_time desc",
            countQuery="select count(*) from help_maintain m " +
                    "left join help_task t on t.id=m.help_task_id " +
                    " group by m.id order by m.create_time desc",
            nativeQuery = true)
    Page<HelpMaintainModel> selectHelpMaintainList(Pageable pageable);



    @Query(value = "select * from help_maintain where id=:helpMaintainId",nativeQuery = true)
    HelpMaintain selectById(@Param("helpMaintainId") Long helpMaintainId);

    @Query(value = "select 1 from help_maintain where help_task_id=:helpTaskId and state=:state limit 1",nativeQuery = true)
    Integer selectCount(@Param("helpTaskId") Long helpTaskId,@Param("state") Integer state);

    @Transactional
    @Modifying
    @Query(value = "update help_maintain set state=:state,content=:content where id=:helpMaintainId",nativeQuery = true)
    Integer updateState(@Param("helpMaintainId") Long helpMaintainId,@Param("state") Integer state,@Param("content") String content);

}
